package org.meb.conquest.rest.service;

import java.util.HashSet;
import java.util.Set;

import lombok.Getter;

import org.apache.commons.lang3.exception.ExceptionUtils;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

public class ExceptionFilter {

	private Throwable base;

	@Getter
	private Throwable throwable;
	private static final Set<String> constraintNames;
	
	static {
		constraintNames = new HashSet<>();
		constraintNames.add(Constant.Constraint.UK_NAME);
		constraintNames.add(Constant.Constraint.UK_MAIL);
		constraintNames.add(Constant.Constraint.UK_HASH);
		constraintNames.add(Constant.Constraint.UK_SALT);
	}
	@Getter
	private String violatedConstraint;

	@Getter
	private String violatedConstraintValue;

	public ExceptionFilter(Throwable base) {
		this.base = base;
	}

	public <T extends Throwable> boolean find(Class<T> type) {
		int index = ExceptionUtils.indexOfType(base, type);
		if (index == -1) {
			return false;
		} else {
			throwable = ExceptionUtils.getThrowables(base)[index];
			return true;
		}
	}

	public boolean findViolatedConstraint() {
		boolean found = find(MySQLIntegrityConstraintViolationException.class);
		if (found) {
			for (String name : constraintNames) {
				if (throwable.getMessage().contains(name)) {
					violatedConstraint = name;
					return true;
				}
			}
		}
		return false;
	}
}
